package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.Connector;

public class Payment {

	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblPayment";
	public int addPayment(int id_customer,int cost_room,int cost_service,int total,int payment,String date,int status){
		connect = Connector.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_customer,cost_room,cost_service,"
                                + "total,payment,date,status) VALUES (?,?,?,?,?,?,?)",pstm.RETURN_GENERATED_KEYS);
			pstm.setInt(1,id_customer);
			pstm.setInt(2,cost_room);
			pstm.setInt(3,cost_service);
			pstm.setInt(4,total);
			pstm.setInt(5,payment);
			pstm.setString(6,date);
			pstm.setInt(7,status);
			if(pstm.executeUpdate()>0){
                                pstm.getGeneratedKeys();
				ResultSet rs=pstm.getGeneratedKeys();
				if (rs.next()) {
				    return rs.getInt(1);
				}
				
			}
			
		}catch(SQLException e){
			
		}
		return 0;
		
	}
	public boolean deletePayment(int id){
		connect = Connector.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean updatePayment(int id_customer,int cost_room,int cost_service,int total,int payment,String date,int status){
		connect= Connector.getConnect();
		try{ 
			pstm = connect.prepareStatement("UPDATE "+this.table +" SET cost_room=?,cost_service=?,total=?,payment=?" +
					",date=?,status=?  WHERE id_customer=?");
			pstm.setInt(7,id_customer);
			pstm.setInt(1,cost_room);
			pstm.setInt(2,cost_service);
			pstm.setInt(3,total);
			pstm.setInt(4,payment);
			pstm.setString(5,date);
			pstm.setInt(6,status);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Payment> getAllPayment(){
		connect= Connector.getConnect();
		ArrayList<entity.Payment> list= new ArrayList<entity.Payment>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();                        
			while(rs.next()){                              
				if(getPayment(rs)!=null) list.add(getPayment(rs));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public entity.Payment getPaymentById(int id){
		
		connect= Connector.getConnect();
                entity.Payment payment = null ;
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
                        if(rs.next()){
                          if(getPayment(rs)!=null)   return getPayment(rs);
                        
                        }
	// Lay thong tin gui den controller
		}catch(SQLException e){
			e.printStackTrace();
		}
                return payment;
	}
        	public entity.Payment getPaymentByIdCustomer(int id){
		
		connect= Connector.getConnect();
                entity.Payment payment = null ;
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id_customer=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
                        if(rs.next()){
                          if(getPayment(rs)!=null)     return getPayment(rs);
                        
                        }
	// Lay thong tin gui den controller
		}catch(SQLException e){
			e.printStackTrace();
		}
                return payment;
	}
                public entity.Payment getPayment(ResultSet rs) throws SQLException{
                                Customer customer = new Customer();
				entity.Payment payment = new entity.Payment();
                                payment.setId(rs.getInt("id"));
				payment.setId_customer(rs.getInt("id_customer"));
				payment.setCost_room(rs.getInt("cost_room"));
				payment.setCost_service(rs.getInt("cost_service"));
				payment.setTotal(rs.getInt("total"));
				payment.setPayment(rs.getInt("payment"));
				payment.setDate(rs.getString("date"));
				payment.setStatus(rs.getInt("status"));
                               if(customer.getCustomerById(rs.getInt("id_customer"))!=null){
                                   payment.setCustomer(customer.getCustomerById(rs.getInt("id_customer"))); 
                                    return payment;
                               }else
                                   return null;
                        
                }
}
